package com.edais.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class Log {
	private static final org.apache.commons.logging.Log logger = LogFactory.getLog(Log.class);

	public void doBefore(JoinPoint joinpoint) {
	}

	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		String logInfo = pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName();
		long time = System.currentTimeMillis();
		logger.info(logInfo + "开始执行 ");
		Object returnObject = pjp.proceed();
		logger.info(logInfo + "执行时间： " + (System.currentTimeMillis() - time) + " ms");
		logger.info(logInfo + "入参： ");
		Object[] args = pjp.getArgs();
		if (args != null) {
			for (Object obj : args) {
				if (obj != null) {
					Field[] field = obj.getClass().getDeclaredFields();
					if (field != null) {
						try {
							this.doGetObjValue(obj, field);
						} catch (Exception e) {
						}
					}
				logger.info(obj.getClass() + " ：" + obj.toString());
				}
			}
		}
		logger.info(logInfo + "出参： ");
		if (returnObject != null) {
			Field[] field = returnObject.getClass().getDeclaredFields();
			if (field != null) {
				try {
					this.doGetObjValue(returnObject, field);
				} catch (Exception e) {
				}
			}
			logger.info(returnObject.getClass() + " ：" + returnObject.toString());
		}
		return returnObject;
	}

	public void doAfter(JoinPoint joinpoint, Object returnObj) {
	}

	private void doGetObjValue(Object model, Field[] field) throws Exception {
		String[] modelName = new String[field.length];
		String[] modelType = new String[field.length];
		for (int i = 0; i < field.length; i++) {
			String name = field[i].getName();
			modelName[i] = name;
			String type = field[i].getGenericType().toString();
			modelType[i] = type;
			field[i].setAccessible(true);
			name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
			if (type.equals("class java.lang.String")) {
				Method m = model.getClass().getMethod("get" + name);
				String value = (String) m.invoke(model);
				if (value != null) {
					logger.info(name + " ： " + value);
				}
			}
			if (type.equals("class java.lang.Integer")) {
				Method m = model.getClass().getMethod("get" + name);
				Integer value = (Integer) m.invoke(model);
				if (value != null) {
					logger.info(name + " ： " + value);
				}
			}
			if (type.equals("class java.lang.Short")) {
				Method m = model.getClass().getMethod("get" + name);
				Short value = (Short) m.invoke(model);
				if (value != null) {
					logger.info(name + " ： " + value);
				}
			}
			if (type.equals("class java.lang.Double")) {
				Method m = model.getClass().getMethod("get" + name);
				Double value = (Double) m.invoke(model);
				if (value != null) {
					logger.info(name + " ： " + value);
				}
			}
			if (type.equals("class java.lang.Boolean")) {
				Method m = model.getClass().getMethod("get" + name);
				Boolean value = (Boolean) m.invoke(model);
				if (value != null) {
					logger.info(name + " ： " + value);
				}
			}
			if (type.equals("class java.util.Date")) {
				Method m = model.getClass().getMethod("get" + name);
				Date value = (Date) m.invoke(model);
				if (value != null) {
					logger.info(name + " ： " + value.toGMTString());
				}
			}
		}
	}
}